<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Notify extends Base
{

	public function index()
	{
		return view('lists');
	}

	public function notify_admin()
	{
		$category = config('param.notify_category');
		$this->assign('notify_category', $category);
		return view();
	}

	public function lists()
	{
		$map = array();
		$data = array();
		$post = $this->request->param();
		if (empty($post['title']) || $post['title'] == '') {
		} else {
			$map[] = ['notify.title', 'like', '%' . $post['title'] . '%'];
		}
		if (empty($post['category_id']) || $post['category_id'] == '') {
		} else {
			$map[] = ['notify.category_id', '=', $post['category_id']];
		}
		$map[] = ['notify_status.uid', '=', session('uid')];
		$data['count'] = Db::view('notify', 'id,title,info_id,c_time,category_id')
			->view('notify_status', 'uid,notify_id,status', 'notify.id=notify_status.notify_id', 'LEFT')
			->where($map)
			->count();
		$data['data'] = Db::view('notify', 'id,title,info_id,c_time,category_id')
			->view('notify_status', 'uid,notify_id,status', 'notify.id=notify_status.notify_id', 'LEFT')
			->where($map)
			// ->fetchSql(true)
			->order('id asc')
			->page(input("param.page"), input('param.limit'))
			->select();
		$category = config('param.notify_category');
		//dump($data);
		foreach ($data['data'] as &$info) {
			$info['c_time'] = date('Y-m-d H:i:s', $info['c_time']);
			if ($info['status'] == 1) {
				$info['category'] = '<span class="layui-badge-dot"></span>' . $category[$info['category_id']];
			} else {
				$info['category'] = $category[$info['category_id']];
			}
		}
		if ($data['count'] > 0 && $data['data']) {
			$data['msg'] = '获取成功';
		} else {
			$data['msg'] = '获取数据为空';
		}
		$data['code'] = 0;
		return json($data);
	}

	public function notify_add()
	{
		if ($this->request->isPost()) {
			$post = $this->request->param();
			$validate = new \app\admin\validate\Doc();
			if (!$validate->check($post)) {
				$this->error($validate->getError());
			}
			Db::startTrans();
			try {
				$post['info_id'] = Db::name('infoview')->insertGetId(['info' => $this->request->param('info', '', null)]);
				unset($post['info']);
				unset($post['id']);
				unset($post['editormd-html-code']);
				$post['c_time'] = time();
				$post['groups'] = implode(',', $post['group']);
				$notify_id = Db::name('notify')->strict(false)->insertGetId($post);
				$list = Db::view('admin', 'id,status')
					->view('auth_group_access', 'uid,group_id', 'admin.id=auth_group_access.uid')
					->where('admin.status', '=', 1)
					->where('auth_group_access.group_id', 'in', $post['groups'])
					->select();
				$data = [];
				foreach ($list as $info) {
					$item = [];
					$item['uid'] = $info['uid'];
					$item['status'] = 1;
					$item['notify_id'] = $notify_id;
					$data[] = $item;
				}
				DB::name('notify_status')->insertAll($data);


				Db::commit();
				$status = 1;
			} catch (\Exception $e) {
				Db::rollback();
				$status = 0;
			}
			if ($status) {
				$this->success('操作成功!');
			}
			$this->error('操作失败');
		} else {
			//获取用户组
			$group = Db::name('auth_group')->field('id,title,status')->where('id', 'neq', '1')->select();
			$this->assign('group', $group);
			$category = config('param.notify_category');
			$this->assign('notify_category', $category);
			return view();
		}
	}

	public function notifydelete()
	{
		if ($this->request->isPost()) {
			$post = $this->request->param();
			if (!isset($post['id'])) {
				$this->error('请求参数错误');
			}
			Db::startTrans();
			try {
				$info = Db::name('notify')->where(['id' => $post['id']])->find();
				$infoview = Db::name('infoview')->where(['id' => $info['info_id']])->value('info');
				$qiniu = new Qiniu();
				preg_match_all('/(?=\/qiniu).+?(?=")/', $infoview, $files);
				foreach ($files[0] as $item) {
					$qiniu->delqiniu($item);
				}
				Db::name('infoview')->where(['id' => $info['info_id']])->delete();
				Db::name('notify')->where(['id' => $post['id']])->delete();
				DB::name('notify_status')->where('notify_id', $info['id'])->delete();
				Db::commit();
				$status = 1;
			} catch (\Exception $e) {
				Db::rollback();
				$status = 0;
			}
			if ($status) {
				$this->success('操作成功!');
			}
			$this->error('操作失败');

		}
	}

	public function notifyview()
	{
		$post = $this->request->param();
		if ($this->request->isPost()) {
			$validate = new \app\admin\validate\Doc();
			if (!$validate->check($post)) {
				$this->error($validate->getError());
			}
			Db::startTrans();
			try {
				Db::name('infoview')->where(['id' => $post['info_id']])->update(['info' => $this->request->param('info', '', null)]);
				unset($post['info']);
				Db::name('notify')->where(['id' => $post['id']])->update($post);
				Db::commit();
				$status = 1;
			} catch (\Exception $e) {
				Db::rollback();
				$status = 0;
			}
			if ($status) {
				$this->success('操作成功!');
			}
			$this->error('操作失败');
		} else {
			$info = Db::name('notify')
				->where('id', input('id'))
				->find();
			$info['info'] = Db::name('infoview')
				->where('id', $info['info_id'])
				->value('info');
			DB::name('notify_status')
				->where('notify_id', input('id'))
				->where('uid', session('uid'))
				->update(['status' => 2, 'c_time' => time()]);
			//dump($info);
			$info['c_time'] = date('Y-m-d H:i:s', $info['c_time']);
			$this->assign('info', $info);
			return view();
		}
	}

	public function notify_edit()
	{
		if ($this->request->isPost()) {
			$post = $this->request->param();
			$validate = new \app\admin\validate\Doc();
			if (!$validate->check($post)) {
				$this->error($validate->getError());
			}

			Db::startTrans();
			try {
				Db::name('infoview')->where(['id' => $post['info_id']])->update(['info' => $this->request->param('info', '', null)]);
				unset($post['info']);
				$post['c_time'] = time();
				unset($post['editormd-html-code']);
				$post['groups'] = implode(',', $post['group']);
				unset($post['group']);
				Db::name('notify')->where(['id' => $post['id']])->update($post);
				$list = Db::view('admin', 'id,status')
					->view('auth_group_access', 'uid,group_id', 'admin.id=auth_group_access.uid')
					->where('admin.status', '=', 1)
					->where('auth_group_access.group_id', 'in', $post['groups'])
					->select();
				// dump($post['groups']);
				//dump($list);
				$data = [];
				DB::name('notify_status')->where('notify_id', $post['id'])->delete();
				foreach ($list as $info) {
					$item = [];
					$item['uid'] = $info['uid'];
					$item['status'] = 1;
					$item['notify_id'] = $post['id'];
					$data[] = $item;
				}
				DB::name('notify_status')->insertAll($data);

				Db::commit();
				$status = 1;
			} catch (\Exception $e) {
				Db::rollback();
				$status = 0;
			}
			if ($status) {
				$this->success('操作成功!');
			}
			$this->error('操作失败');
		} else {
			//获取用户组
			$group = Db::name('auth_group')->field('id,title,status')->where('id', 'neq', '1')->select();
			//dump($group);
			$this->assign('group', $group);
			$category = config('param.notify_category');
			$this->assign('notify_category', $category);
			$info = Db::name('notify')
				->where('id', input('param.id'))
				->find();
			//$info['groups']=explode(',',$info['groups']);
			$info['info'] = Db::name('infoview')
				->where('id', $info['info_id'])
				->value('info');
			//dump($info);
			$users = DB::view('notify_status', 'uid,notify_id,status')
				->view('admin', 'id,nickname', 'notify_status.uid=admin.id')
				->where('notify_id', $info['id'])
				->select();
			//dump($users);
			$this->assign('users', $users);
			$this->assign('info', $info);
			return view('notify_add');
		}
	}
}